package com.example.wpx.util;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

/**
 * @Author: Duanjc
 * @Description:
 * @Data: Create in 4:26 2017/9/26.
 * @Modified By:
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保證序列化json如果是null的对象，key也消失
public class ServerResponse<T> implements Serializable {
    private String status;
    private String msg;
    private T data;

    private ServerResponse(String status){
        this.status = status;
    }

    private ServerResponse(String status,T data){
        this.status = status;
        this.data = data;
    }

    private ServerResponse(String status,String msg,T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
    private ServerResponse(String status,String msg){
        this.status = status;
        this.msg = msg;
    }

    @JsonIgnore
    //使之不在json序列化中
    public boolean isSuccess(){
        return this.status.equals(ResponseCode.SUCCESS.getCode());
    }

    public String getStatus(){
        return status;
    }
    public String getMsg(){
        return msg;
    }
    public T getData(){
        return data;
    }

    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
    public static <T> ServerResponse<T> createBySuccess(T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }
    public static <T> ServerResponse<T> createBySuccess(String msg,T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> ServerResponse<T> createByError(){
        return new ServerResponse<T>(ResponseCode.Error.getCode());
    }
    public static <T> ServerResponse<T> createByErrorMessage(String msg){
        return new ServerResponse<T>(ResponseCode.Error.getCode(),msg);
    }

    public static  <T> ServerResponse<T> careateByErrorCodeMessage(String errorCode,String errorMessage){
        return new  ServerResponse<T>(errorCode,errorMessage);
    }

}
